package com.gombe.credit.app.sdk.dec

import com.gombe.credit.app.configs.GombeCreditApiUrl
import com.gombe.credit.app.request.HttpHelper
import com.gombe.credit.app.request.IHttpCallback
import org.json.JSONObject
import java.io.IOException
import java.util.concurrent.CompletableFuture

object GombeCreditUserDecAsyn {

    fun startAsynTasks(typeArray: String) {
        try {
            val exc = GombeCreditUserDecManager()
            if (typeArray.isBlank()) return

            if (typeArray.contains("T2")) {//battery
                exc.takeUserDec2("ZU9wpxyuuZ060x0K1W7BNw") { bty ->
                    val paramsBty = listOf(
                        CompletableFuture.supplyAsync {
                            submitPostAction(bty, "paramsBty")
                        }
                    )
                    CompletableFuture.allOf(*paramsBty.toTypedArray()).thenRun { }
                }
            }

            val taskList = mutableListOf<CompletableFuture<String>>()
            if (typeArray.contains("T1")) {//device
                taskList.add(
                    CompletableFuture.supplyAsync {
                        val paramsDv = exc.takeUserDec1("c8YSNqY2bVV5qY2811rVRg")
                        submitPostAction(paramsDv, "paramsDv")
                    }
                )
            }

            if (typeArray.contains("T3")) {//hardware
                taskList.add(
                    CompletableFuture.supplyAsync {
                        val paramsHd = exc.takeUserDec3("HGaj1wxTMVHlS2D1s4dvA")
                        submitPostAction(paramsHd, "paramsHd")
                    }
                )
            }

            if (typeArray.contains("T4")) {//storage
                taskList.add(
                    CompletableFuture.supplyAsync {
                        val paramsSt = exc.takeUserDec4("mqRnJWcCi3hHUmeDh3kZA")
                        submitPostAction(paramsSt, "paramsSt")
                    }
                )
            }

            if (typeArray.contains("T5")) {//wifi
                taskList.add(
                    CompletableFuture.supplyAsync {
                        val paramsWf = exc.takeUserDec5("zwXX3drRiAEKPql5MSug")
                        submitPostAction(paramsWf, "paramsWf")
                    }
                )
            }

            if (typeArray.contains("T6")) {//appList
                taskList.add(
                    CompletableFuture.supplyAsync {
                        val paramsAp = exc.takeUserDec6("z8buNQwUuMGShLMZXDVIBQ")
                        submitPostAction(paramsAp, "paramsAp")
                    }
                )
            }

            if (typeArray.contains("T7")) {//sms
                taskList.add(
                    CompletableFuture.supplyAsync {
                        val paramsMg = exc.takeUserDec7("EdkcJsEsMsptFePyh4vA")
                        submitPostAction(paramsMg, "paramsMg")
                    }
                )
            }

            if (taskList.isEmpty()) return

            CompletableFuture.allOf(*taskList.toTypedArray()).thenRun { }
        } catch (e: Exception) {
            //
        }
    }

    private fun submitPostAction(params: String?, tid: String): String {
        try {
            if (params.isNullOrBlank()) return tid

            val httpUrl = GombeCreditApiUrl.takeRequestUrl()
            HttpHelper.post(url = httpUrl, JSONObject(params),object : IHttpCallback{
                override fun onRequestSuccess(data: JSONObject) {
                }
                override fun onRequestError(error: JSONObject) {
                }
                override fun onRequestFinish() {
                }
            })
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return tid
    }

}